package cn.juque.jdfsystem.helper;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.juque.common.constants.MessageEnum;
import cn.juque.common.exception.AppException;
import cn.juque.jdfsystem.cache.RegisterInfoCache;
import cn.juque.jdfsystem.config.JdfConfig;
import cn.juque.jdfsystem.entity.RegisterInfo;
import cn.juque.jdfsystem.enums.RegisterExecuteStatusEnum;
import cn.juque.jdfsystem.mapper.RegisterInfoMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author juque
 * @version 1.0.0
 * <ul>
 *     <li>RegisterInfoHelper</li>
 * </ul>
 * @date 2023-01-11 16:17:00
 **/
@Component("registerInfoHelper")
public class RegisterInfoHelper {

    public static final String BEAN = "registerInfoHelper";

    @Resource
    private RegisterInfoCache registerInfoCache;

    @Resource
    private RegisterInfoMapper registerInfoMapper;

    @Resource
    private JdfConfig jdfConfig;

    /**
     * 获取有效ip
     *
     * @return List
     */
    public List<String> getValidOtherIp() {
        Map<String, RegisterInfo> map = this.registerInfoCache.getAllRegister();
        return map.values().stream()
                .filter(f -> !RegisterExecuteStatusEnum.DOWN_LINE.getCode().equals(f.getExecuteStatus()))
                .map(RegisterInfo::getIp)
                .filter(ip -> !this.jdfConfig.getBindIp().equals(ip))
                .distinct().collect(Collectors.toList());
    }

    /**
     * 获取所有ip
     *
     * @return List
     */
    public List<String> getAllIp() {
        Map<String, RegisterInfo> map = this.registerInfoCache.getAllRegister();
        return map.values().stream()
                .map(RegisterInfo::getIp)
                .distinct().collect(Collectors.toList());
    }

    /**
     * 获取有效节点信息
     *
     * @param nodeName 节点名称
     * @return RegisterInfo
     */
    public RegisterInfo getValidRegisterInfo(String nodeName) {
        Assert.notEmpty(nodeName);
        Map<String, RegisterInfo> map = this.registerInfoCache.getAllRegister();
        RegisterInfo registerInfo = map.get(nodeName);
        if (null == registerInfo) {
            RegisterInfo query = new RegisterInfo();
            query.setNodeName(nodeName);
            List<RegisterInfo> list = this.registerInfoMapper.selectList(new QueryWrapper<>(query));
            if (CollUtil.isEmpty(list)) {
                throw new AppException(MessageEnum.PARAM_NULL.getCode(), "【" + nodeName + "】节点不存在");
            }
            registerInfo = list.get(0);
            this.registerInfoCache.update(registerInfo);
        }
        if (RegisterExecuteStatusEnum.EXCEPTION.getCode().equals(registerInfo.getExecuteStatus())) {
            throw new AppException(MessageEnum.PARAM_NULL.getCode(), "【" + nodeName + "】节点异常，不允许使用");
        }
        if (RegisterExecuteStatusEnum.DOWN_LINE.getCode().equals(registerInfo.getExecuteStatus())) {
            throw new AppException(MessageEnum.PARAM_NULL.getCode(), "【" + nodeName + "】节点已下线，不允许使用");
        }
        return registerInfo;
    }
}
